// ==UserScript==
// @name       备注
// @namespace  npm/vite-plugin-monkey
// @version    0.0.0
// @author     monkey
// @icon       https://vitejs.dev/logo.svg
// @match      *://*/*
// @require    https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.18.5/xlsx.full.min.js
// @require    https://cdn.jsdelivr.net/npm/vue@3.4.31/dist/vue.global.prod.js
// @grant      GM_addStyle
// ==/UserScript==

(t=>{if(typeof GM_addStyle=="function"){GM_addStyle(t);return}const o=document.createElement("style");o.textContent=t,document.head.append(o)})(" #float-ball{position:fixed;bottom:20px;right:20px;width:50px;height:50px;background-color:#007bff;color:#fff;border-radius:50%;display:flex;justify-content:center;align-items:center;cursor:pointer;z-index:10001}#float-ball:hover{background-color:#0056b3}.toggle-button[data-v-fc41f726]{position:fixed;top:50%;right:0;transform:translateY(-50%);z-index:10000;padding:10px;background-color:transparent;color:#aab0aa;border:none;cursor:pointer;display:flex;justify-content:center;align-items:center;width:40px;height:40px;transition:color .3s ease}.toggle-button[data-v-fc41f726]:hover{color:#aab0aa}.toggle-button i[data-v-fc41f726]{font-size:24px}.sidebar-container[data-v-fc41f726]{position:fixed;top:0;right:-400px;width:400px;height:100%;background-color:#f9f9f9;box-shadow:0 0 10px #00000080;transition:right .3s;z-index:10000;display:flex;flex-direction:column}.sidebar-content[data-v-fc41f726]{display:flex;flex-direction:column;height:100%;padding:20px}.notes-container[data-v-fc41f726]{flex-grow:1;overflow-y:auto;margin-bottom:10px}.input-container[data-v-fc41f726]{margin-top:auto}.note-input[data-v-fc41f726]{width:100%;height:100px;padding:10px;box-sizing:border-box;border:1px solid #ccc;border-radius:5px;resize:vertical}.copy-success-message[data-v-fc41f726]{position:fixed;top:20px;left:50%;transform:translate(-50%);padding:10px;background-color:#fff;color:#333;border:1px solid #ccc;border-radius:5px;box-shadow:0 0 10px #0000001a;z-index:10000}.note-item[data-v-fc41f726]{position:relative;padding:10px;margin-bottom:10px;border:1px solid #ccc;border-radius:5px;background-color:#fff;display:flex;justify-content:space-between;align-items:center}.button-container[data-v-fc41f726]{display:flex;gap:5px}.button-container button[data-v-fc41f726]{padding:5px;background-color:transparent;border:none;cursor:pointer}.button-container button[data-v-fc41f726]:nth-child(1){color:#f44336}.button-container button[data-v-fc41f726]:nth-child(2){color:#2196f3}.button-container button[data-v-fc41f726]:nth-child(3){color:#ffc107} ");

(function (vue) {
  'use strict';

  const _sfc_main$1 = {
    setup() {
      const notes = vue.ref([]);
      const newNote = vue.ref("");
      const sidebarRight = vue.ref("-400px");
      const showCopyMessage = vue.ref(false);
      const currentUrl = vue.ref("");
      const addFontAwesome = () => {
        const link = document.createElement("link");
        link.rel = "stylesheet";
        link.href = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css";
        document.head.appendChild(link);
      };
      const toggleSidebar = () => {
        sidebarRight.value = sidebarRight.value === "0px" ? "-400px" : "0px";
      };
      const loadNotes = () => {
        const savedNotes = JSON.parse(localStorage.getItem(`notes_${currentUrl.value}`) || "[]");
        notes.value = savedNotes.map((text) => ({ text, isEditing: false }));
      };
      const deleteNote = (index) => {
        notes.value.splice(index, 1);
        saveNotes();
      };
      const copyNote = (text) => {
        if (navigator.clipboard && navigator.clipboard.writeText) {
          navigator.clipboard.writeText(text).then(() => {
            showCopyMessage.value = true;
            setTimeout(() => {
              showCopyMessage.value = false;
            }, 2e3);
          }).catch((err) => {
            console.error("复制失败:", err);
            fallbackCopyTextToClipboard(text);
          });
        } else {
          fallbackCopyTextToClipboard(text);
        }
      };
      const fallbackCopyTextToClipboard = (text) => {
        const textArea = document.createElement("textarea");
        textArea.value = text;
        textArea.style.top = "0";
        textArea.style.left = "0";
        textArea.style.position = "fixed";
        document.body.appendChild(textArea);
        textArea.focus();
        textArea.select();
        try {
          const successful = document.execCommand("copy");
          if (successful) {
            showCopyMessage.value = true;
            setTimeout(() => {
              showCopyMessage.value = false;
            }, 2e3);
          } else {
            alert("复制失败，请手动复制");
          }
        } catch (err) {
          console.error("复制失败:", err);
          alert("复制失败，请手动复制");
        }
        document.body.removeChild(textArea);
      };
      const startEditing = (index) => {
        notes.value[index].isEditing = true;
      };
      const saveEdit = (index) => {
        notes.value[index].isEditing = false;
        saveNotes();
      };
      const saveNotes = () => {
        localStorage.setItem(`notes_${currentUrl.value}`, JSON.stringify(notes.value.map((note) => note.text)));
      };
      const handleOutsideClick = (event) => {
        const sidebar = document.querySelector(".sidebar-container");
        const toggleButton = document.querySelector(".toggle-button");
        console.log(sidebarRight.value, sidebar, toggleButton);
        if (sidebarRight.value === "0px" && sidebar && toggleButton && !sidebar.contains(event.target) && !toggleButton.contains(event.target)) {
          console.log(sidebarRight.value, sidebar, toggleButton);
          toggleSidebar();
        }
      };
      const saveNewNote = () => {
        if (newNote.value.trim() !== "") {
          notes.value.push({ text: newNote.value, isEditing: false });
          saveNotes();
          newNote.value = "";
        }
      };
      vue.onMounted(() => {
        currentUrl.value = window.location.href;
        loadNotes();
        addFontAwesome();
        document.addEventListener("click", handleOutsideClick);
      });
      vue.onBeforeUnmount(() => {
        document.removeEventListener("click", handleOutsideClick);
      });
      return {
        notes,
        newNote,
        sidebarRight,
        showCopyMessage,
        toggleSidebar,
        saveNewNote,
        deleteNote,
        copyNote,
        startEditing,
        saveEdit
      };
    }
  };
  const _export_sfc = (sfc, props) => {
    const target = sfc.__vccOpts || sfc;
    for (const [key, val] of props) {
      target[key] = val;
    }
    return target;
  };
  const _withScopeId = (n) => (vue.pushScopeId("data-v-fc41f726"), n = n(), vue.popScopeId(), n);
  const _hoisted_1 = /* @__PURE__ */ _withScopeId(() => /* @__PURE__ */ vue.createElementVNode("i", { class: "fas fa-sticky-note" }, null, -1));
  const _hoisted_2 = [
    _hoisted_1
  ];
  const _hoisted_3 = { class: "sidebar-content" };
  const _hoisted_4 = { class: "notes-container" };
  const _hoisted_5 = ["onClick"];
  const _hoisted_6 = ["onUpdate:modelValue", "onBlur"];
  const _hoisted_7 = { class: "button-container" };
  const _hoisted_8 = ["onClick"];
  const _hoisted_9 = /* @__PURE__ */ _withScopeId(() => /* @__PURE__ */ vue.createElementVNode("i", { class: "fas fa-trash-alt" }, null, -1));
  const _hoisted_10 = [
    _hoisted_9
  ];
  const _hoisted_11 = ["onClick"];
  const _hoisted_12 = /* @__PURE__ */ _withScopeId(() => /* @__PURE__ */ vue.createElementVNode("i", { class: "fas fa-copy" }, null, -1));
  const _hoisted_13 = [
    _hoisted_12
  ];
  const _hoisted_14 = ["onClick"];
  const _hoisted_15 = /* @__PURE__ */ _withScopeId(() => /* @__PURE__ */ vue.createElementVNode("i", { class: "fas fa-edit" }, null, -1));
  const _hoisted_16 = [
    _hoisted_15
  ];
  const _hoisted_17 = { class: "input-container" };
  const _hoisted_18 = { class: "copy-success-message" };
  function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
    return vue.openBlock(), vue.createElementBlock("div", null, [
      vue.createElementVNode("button", {
        class: "toggle-button",
        onClick: _cache[0] || (_cache[0] = (...args) => $setup.toggleSidebar && $setup.toggleSidebar(...args)),
        title: "打开/关闭备注"
      }, _hoisted_2),
      vue.createElementVNode("div", {
        class: "sidebar-container",
        style: vue.normalizeStyle({ right: $setup.sidebarRight })
      }, [
        vue.createElementVNode("div", _hoisted_3, [
          vue.createElementVNode("div", _hoisted_4, [
            (vue.openBlock(true), vue.createElementBlock(vue.Fragment, null, vue.renderList($setup.notes, (note, index) => {
              return vue.openBlock(), vue.createElementBlock("div", {
                key: index,
                class: "note-item"
              }, [
                !note.isEditing ? (vue.openBlock(), vue.createElementBlock("span", {
                  key: 0,
                  onClick: ($event) => $setup.startEditing(index)
                }, vue.toDisplayString(note.text), 9, _hoisted_5)) : vue.withDirectives((vue.openBlock(), vue.createElementBlock("textarea", {
                  key: 1,
                  "onUpdate:modelValue": ($event) => note.text = $event,
                  onBlur: ($event) => $setup.saveEdit(index)
                }, null, 40, _hoisted_6)), [
                  [vue.vModelText, note.text]
                ]),
                vue.createElementVNode("div", _hoisted_7, [
                  vue.createElementVNode("button", {
                    onClick: vue.withModifiers(($event) => $setup.deleteNote(index), ["stop"]),
                    title: "删除"
                  }, _hoisted_10, 8, _hoisted_8),
                  vue.createElementVNode("button", {
                    onClick: vue.withModifiers(($event) => $setup.copyNote(note.text), ["stop"]),
                    title: "复制"
                  }, _hoisted_13, 8, _hoisted_11),
                  vue.createElementVNode("button", {
                    onClick: vue.withModifiers(($event) => $setup.startEditing(index), ["stop"]),
                    title: "编辑"
                  }, _hoisted_16, 8, _hoisted_14)
                ])
              ]);
            }), 128))
          ]),
          vue.createElementVNode("div", _hoisted_17, [
            vue.withDirectives(vue.createElementVNode("textarea", {
              "onUpdate:modelValue": _cache[1] || (_cache[1] = ($event) => $setup.newNote = $event),
              class: "note-input",
              placeholder: "输入新备注",
              onBlur: _cache[2] || (_cache[2] = (...args) => $setup.saveNewNote && $setup.saveNewNote(...args))
            }, null, 544), [
              [vue.vModelText, $setup.newNote]
            ])
          ])
        ])
      ], 4),
      vue.withDirectives(vue.createElementVNode("div", _hoisted_18, "备注已拷贝", 512), [
        [vue.vShow, $setup.showCopyMessage]
      ])
    ]);
  }
  const Comment = /* @__PURE__ */ _export_sfc(_sfc_main$1, [["render", _sfc_render], ["__scopeId", "data-v-fc41f726"]]);
  const _sfc_main = /* @__PURE__ */ vue.defineComponent({
    __name: "App",
    setup(__props) {
      return (_ctx, _cache) => {
        return vue.openBlock(), vue.createBlock(Comment);
      };
    }
  });
  vue.createApp(_sfc_main).mount(
    (() => {
      const app = document.createElement("div");
      document.body.append(app);
      return app;
    })()
  );

})(Vue);